Ext.namespace("YX");

var LoginInfo = {};
LoginInfo.USERNAME = "";
LoginInfo.ORGCODE = "";
LoginInfo.LANGUAGE = "";
LoginInfo.TOKEN = "";

var T = {};
T.label = {};
T.msg = {};

Ext.JSON.encodeDate = function(d) {
	return Ext.Date.format(d, '"YmdHis"');
};

function Report() {
}

Report.preview = function(rpt, data) {
	if (Ext.isEmpty(rpt)) {
		return;
	}

	if (Ext.String.endsWith('.grf', true)) {
		rpt = rpt.substr(0, rpt.length - 4);
	}

	var extall = Ext.getHead().query('script');
	var appFolder = "";
	if (!Ext.isEmpty(extall)) {
		var src = extall[0].src.replace("http://", "");
		appFolder = src.substring(src.indexOf("/", 1) + 1, src.indexOf("/", src
		      .indexOf("/")
		      + 1));
	}

	if (rpt.indexOf("/") < 0) {
		rpt = appFolder + "/rpt/template/" + rpt;
	}

	Ext.Ajax.request({
		  url : rpt,
		  success : function(response) {
			  rpt = response.responseText
			  if (!Ext.isEmpty(rpt)) {
				  webapp_ws_run({
					    type : 'preview'
				    }, rpt, Ext.isObject(data) ? Ext.encode(data) : data);
			  } else {
				  Ext.Msg.show({
					    title : 'error',
					    msg : 'Can not load report ' + rpt,
					    icon : Ext.Msg.ERROR,
					    buttons : Ext.Msg.OK
				    });
			  }
		  },
		  failure : function() {
			  Ext.Msg.show({
				    title : 'error',
				    msg : 'Can not load report ' + rpt,
				    icon : Ext.Msg.ERROR,
				    buttons : Ext.Msg.OK
			    });
		  }
	  });
}

Report.print = function(rpt, data) {
	if (Ext.isEmpty(rpt)) {
		return;
	}

	if (Ext.String.endsWith('.grf', true)) {
		rpt = rpt.substr(0, rpt.length - 4);
	}

	var extall = Ext.getHead().query('script');
	var appFolder = "";
	if (!Ext.isEmpty(extall)) {
		var src = extall[0].src.replace("http://", "");
		appFolder = src.substring(src.indexOf("/", 1) + 1, src.indexOf("/", src
		      .indexOf("/")
		      + 1));
	}

	if (rpt.indexOf("/") < 0) {
		rpt = appFolder + "/rpt/template/" + rpt;
	}

	Ext.Ajax.request({
		  url : rpt,
		  success : function(response) {
			  rpt = response.responseText
			  if (!Ext.isEmpty(rpt)) {
				  webapp_ws_run({
					    type : 'print'
				    }, rpt, Ext.isObject(data) ? Ext.encode(data) : data);
			  } else {
				  Ext.Msg.show({
					    title : 'error',
					    msg : 'Can not load report ' + rpt,
					    icon : Ext.Msg.ERROR,
					    buttons : Ext.Msg.OK
				    });
			  }
		  },
		  failure : function() {
			  Ext.Msg.show({
				    title : 'error',
				    msg : 'Can not load report ' + rpt,
				    icon : Ext.Msg.ERROR,
				    buttons : Ext.Msg.OK
			    });
		  }
	  });
}

Report.exportExcel = function(fileName) {
	var nWin = window
	  .open(
	    'export?fileName=' + fileName,
	    '_blank',
	    'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,revisable=no,left=100,top=0,width=600,height=600');
	new Ext.util.DelayedTask(function() {
		  nWin.opener = null;
		  nWin.close();
	  }).delay(15000);
	//nWin.opener = null;
	//nWin.close();
}